package com.mj.action.system;


import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.mj.base.BaseController;
import com.mj.model.system.Buttons;
import com.mj.service.system.ButtonsService;

/**
 * 按钮权限管理
 * @author MJ002
 *2018年10月9日
 */
@Controller
@RequestMapping("buttons")
public class ButtonsAction extends BaseController{
	@Resource
	private ButtonsService btnService;
	
	
    
	//列表页面
	@RequestMapping(value = "")
	public String toDeptList(Model model){
		return "system/buttons/buttons_list"; 
	}
	
	/**
	 * 列表，grid形式
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="buttonsGrid")
	public String deptGrid(){
    	List<Buttons> buttonslist = btnService.findAllButtons();
		JSONArray objarr = JSONArray.fromObject(buttonslist);
        return renderJsonGrid(objarr, buttonslist.size());
	}
	/**
	 * 添加部门
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="addButtons")
	public Boolean addBtn(Buttons Buttons){
		int flag = btnService.sysButtonsAdd(Buttons);
		if(flag > 0){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 软删除
	 *  
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delButtons")
	public Boolean delDept(Buttons buttons){
		buttons.setStatus(Integer.valueOf(request.getParameter("status")));
		buttons.setBtnId(Integer.valueOf(request.getParameter("btnId")));
		int flag = btnService.sysButtonsEdit(buttons);
		if(flag > 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 编辑信息
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="editButtons")
	public Boolean editDept(Buttons buttons){
		int flag = btnService.sysButtonsEdit(buttons);
		if(flag > 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 查询按钮
	 */
	@ResponseBody
	@RequestMapping(value="getButtonsById")
	public String getDeptById(){
		Buttons u = btnService.sysButtonsQuery(Integer.valueOf(request.getParameter("uid")));
		return renderJson(u);
	}
	
	/**
	 * 按钮combobox
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getButtons",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getButtons() throws Exception {
		List<Buttons>  alist = btnService.findAllButtons();
		String strJson = "";
		gson = new GsonBuilder().registerTypeAdapter(Buttons.class,
				new JsonSerializer<Buttons>() {
					@Override
					public JsonElement serialize(Buttons arg0, Type arg1,
							JsonSerializationContext arg2) {
						JsonObject json = new JsonObject();
						json.addProperty("id", arg0.getBtnId());
						json.addProperty("text",arg0.getName());
						return json;
					}
				}).create();
		strJson = gson.toJson(alist);
		return strJson;
	}
}
